package com.chcit.mobile.mvp.presenter;

import android.app.Application;
import android.widget.Button;
import com.alibaba.fastjson.JSONObject;
import com.chcit.mobile.mvp.model.CommonModel;
import com.chcit.mobile.mvp.model.entity.BpartnerBean;
import com.chcit.mobile.mvp.model.entity.ReceiveBean;
import com.chcit.mobile.mvp.model.entity.ResultBean;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import javax.inject.Inject;
import javax.inject.Named;

import com.chcit.mobile.mvp.contract.ReceiveContract;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.List;


@FragmentScope
public class ReceivePresenter extends BasePresenter<ReceiveContract.Model, ReceiveContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    @Named("common")
    CommonModel commModel;
    @Inject
    public ReceivePresenter(ReceiveContract.Model model, ReceiveContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;

    }

    public void getData(JSONObject json) {
        mModel.getReceives(json)
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                })
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .subscribe(new ErrorHandleSubscriber<List<ReceiveBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<ReceiveBean> lists) {
                        mRootView.updateContentList(lists);
                    }
                });
    }
    public void getBpartners( Consumer<List<BpartnerBean>> consumer){
        commModel.getBparenerBeans()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<BpartnerBean>>(mErrorHandler){

                    @Override
                    public void onNext(List<BpartnerBean> bpartnerBeans) {
                        try {
                            consumer.accept(bpartnerBeans);
                        } catch (Exception e) {
                            onError(e);
                        }
                    }
                });
    }
    public void doBatchConfirm(JSONObject json, Action action) {
        mModel.doBatchConfirm(json)
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                })
                .doFinally(() -> {
                    mRootView.hideLoading();
                }).subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
            @Override
            public void onNext(ResultBean resultBean) {
                try {
                    action.run();
                } catch (Exception e) {
                    onError(e);
                }
            }
        });
    }

}
